Skip to content

Log assertions: add assertions for API log errors#1161

Merged
josecelano merged 3 commits into
torrust:developfrom
josecelano:1152-log-assertions-add-assertions-for-api-log-errors
Dec 26, 2024
Merged

Log assertions: add assertions for API log errors#1161
josecelano merged 3 commits into
torrust:developfrom
josecelano:1152-log-assertions-add-assertions-for-api-log-errors

Conversation

@josecelano

@josecelano josecelano commented Dec 26, 2024

Copy link
Copy Markdown
Member

It adds test assertions for log errors in the context of the tracker API.

Sample ERROR log:

cargo test -- --nocapture servers::api::v1::contract::authentication::should_not_authenticate_requests_when_the_token_is_empty
2024-12-26T11:13:00.203024Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error server_socket_addr=127.0.0.1:35409 request_id=922cffeb-ed64-4d56-bbd0-400d6ea6f8d2

We are using the request_id to find the log line because the tracing setup is global (shared by all tests). When the tracker API request is sent, the request_id is injected as an HTTP header.

```
2024-12-26T09:07:18.149759Z ERROR API: response latency_ms=0 status_code=500 Internal Server Error server_socket_addr=127.0.0.1:41579 request_id=44d8c2f6-630d-4eab-a399-65aed1dbc8ab
```
Now you can send a header `x-request-id` to the API.

```rust
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
    .get_request_with_query(
        "stats",
        Query::params([QueryParam::new("token", "")].to_vec()),
        Some(headers_with_request_id(request_id)),
    )
    .await;
```

That ID is recorded in logs. So you can use it to track the request.
@josecelano

Copy link
Copy Markdown
Member Author

ACk 5d49d48

@josecelano josecelano requested a review from da2ce7 December 26, 2024 11:15
@codecov

codecov Bot commented Dec 26, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 76.18%. Comparing base (ce9f388) to head (5d49d48).
Report is 4 commits behind head on develop.

Files with missing lines Patch % Lines
src/servers/apis/routes.rs 40.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1161      +/-   ##
===========================================
+ Coverage    76.07%   76.18%   +0.11%     
===========================================
  Files          174      174              
  Lines        11601    11601              
  Branches     11601    11601              
===========================================
+ Hits          8825     8838      +13     
+ Misses        2613     2606       -7     
+ Partials       163      157       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@josecelano josecelano merged commit 2a8a114 into torrust:develop Dec 26, 2024
@josecelano josecelano linked an issue Dec 26, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log assertions: add assertions for API log errors

1 participant